import 'package:flutter/material.dart';
import 'shopModel.dart';
import 'areaModel.dart';

class ShopScreenProvider with ChangeNotifier {
  
  List<ShopSortModel> _list = [];
  List<ShopSortModel> get list => _list;

  bool _isRefresh = false;

  List<ShopSortModel> _originList =[];

  List<ShopModel> _shopsList = [];
  Map<String,ShopModel> _shopsMap = {};

  List<ShopSortModel> _collectionShopslist = [];
  List<ShopSortModel> get collectionShopslist => _collectionShopslist;

  String _searchText='';
  String get searchText => _searchText;

  List<dynamic> _areaShops = [];
  List<dynamic> get areaShops => _areaShops;

  List<dynamic> _shopTags = [];
  List<dynamic> get shopTags => _shopTags;

  void setShops(List<ShopSortModel> data){
    if (_originList.length > 0 && _isRefresh == false) {
      notifyListeners();
      return;
    }
    _isRefresh = true;

    _list = data;
    _shopsList = [];
    _originList = [];
    _shopsMap = {};
    data.forEach((ShopSortModel sortModel){
      _originList.add(sortModel);
      sortModel.shops.forEach((ShopModel shop){
        _shopsList.add(shop);
        _shopsMap[shop.uuid] = shop;
      });
    });
    notifyListeners();
  }

  /// 刷新门店
  void refreshShops(List<ShopSortModel> data){
    _isRefresh = true;
    setShops(data);
  }

  void searchTextChanged(String text){
    _searchText = text;

    List<ShopModel> filterList = _shopsList.where((item){
      return item.name.toString().contains(text);
    }).toList();

    if (text.length == 0) {
      _list = _originList;
      notifyListeners();
      return;
    }

    if (filterList.length == 0) {
      _list = [];
      notifyListeners();
      return;
    }

    ShopSortModel sortModel = ShopSortModel();
    sortModel.initial = null;
    sortModel.shops = filterList;
    _list = [sortModel];
    notifyListeners();
  }

  void setCollectionShops(List<ShopSortModel> data){
    
    _collectionShopslist = data;
    notifyListeners();
  }

  void collectionStautsChange(String shopUUID){
    if (_shopsMap[shopUUID] != null) {
      ShopModel shopModel = _shopsMap[shopUUID];
      shopModel.isCollection = !shopModel.isCollection;
    }
    notifyListeners();
  }

  void refreshCollectionShops(){
    notifyListeners();
  }

  void setShopTags(List<dynamic> data){
    _shopTags = data;
    notifyListeners();
  }

  void refreshTagShops() {
    notifyListeners();
  }

  void setAreaShop(List<dynamic> data){
    
    _areaShops = data;
    notifyListeners();
  }


}